package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import beans.Restaurant;
import beans.User;

import dao.RestaurantDAO;
import enums.Cities;
import enums.Countries;

@Controller
public class RestaurantsController {

	private RestaurantDAO restaurantDAO;

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}
	
	@RequestMapping(value = "/deleterestaurant={rastaurantId}", method=RequestMethod.GET)
	public ModelAndView deleterestaurant(@PathVariable("rastaurantId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Restaurant restaurant = restaurantDAO.findRestaurantById(id);
		restaurantDAO.deleteRestaurant(restaurant);
		return new ModelAndView("redirect:restauracje.htm");
	}
	
	@RequestMapping(value = "/editrestaurant={rastaurantId}", method=RequestMethod.GET)
	public ModelAndView editrestaurant(@PathVariable("rastaurantId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Restaurant editedRestaurant = restaurantDAO.findRestaurantById(id);;
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("editedRestaurant", editedRestaurant);
		modelMap.addAttribute("cities",Cities.values());
		modelMap.addAttribute("countries", Countries.values());
		return new ModelAndView("editrestaurant", modelMap);
	}
	
	@RequestMapping(value = "/adminrestaurantedit={rastaurantId}", method=RequestMethod.POST)
	public ModelAndView adminuseredit(@PathVariable("rastaurantId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String street = request.getParameter("street");
		String postal_code = request.getParameter("postal_code");
		String telephone = request.getParameter("telephone");
		Restaurant restaurant= restaurantDAO.findRestaurantById(id);
		restaurant.setName(name);
		restaurant.setStreet(street);
		restaurant.setPostal_code(postal_code);
		restaurant.setTelephone(telephone);
		restaurantDAO.UpdateRestaurant(restaurant);
		return new ModelAndView("redirect:restauracje.htm");
	}
	
	
	
	
	
}
